<template>
  <!-- 新增技巧弹层 -->
  <el-dialog
    :title="`${data.id ? '修改' : '新增'}文章`"
    :visible.sync="show"
    width="50%"
  >
    <el-form :model="formModel" :rules="rules" ref="form" label-width="100px">
      <el-form-item label="文章标题：" prop="title">
        <el-input v-model="formModel.title"></el-input>
      </el-form-item>
      <!-- 富文本 -->
      <el-form-item label="文章内容：" prop="articleBody">
        <quill-editor
          v-model="formModel.articleBody"
          :options="editorOptions"
        ></quill-editor>
      </el-form-item>
      <!-- 富文本 -->
      <el-form-item label="视频地址：">
        <el-input v-model="formModel.videoURL"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="confirm()">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { add, update } from '@/api/hmmm/articles'
import { quillEditor } from 'vue-quill-editor' // 调用编辑器
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
export default {
  name: 'ArticlesAdd',
  props: {
    data: {
      default: () => {},
      type: Object
    }
  },
  data() {
    return {
      show: false,
      // 富文本数据
      formModel: {
        id: null,
        title: null,
        articleBody: null,
        videoURL: null
      },
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      },
      editorOptions: {
        placeholder: '',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ]
        }
      }
    }
  },
  components: {
    quillEditor
  },
  computed: {
    // 富文本方法
    editor() {
      return this.$refs.myQuillEditor.quill
    }
  },
  methods: {
    open() {
      this.show = true
      if (this.data.id) {
        const { id, title, articleBody, videoURL } = this.data
        this.formModel = { id, title, articleBody, videoURL }
      } else {
        this.formModel = {
          id: null,
          title: null,
          articleBody: null,
          videoURL: null
        }
      }
      // 清除校验效果
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },
    confirm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (!this.data.id) {
            await add(this.formModel)
          } else {
            this.formModel.id = this.data.id
            await update(this.formModel)
          }
          this.$message.success('操作成功')
          this.show = false
          this.$emit('updateList')
        } else {
          this.$message.error('操作失败')
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .ql-editor {
  min-height: 200px;
}
</style>
